<?php

namespace App\Http\Controllers\Mobile;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\Front\CreatOrderRequest;

use App\Interfaces\AddressService;
use App\Interfaces\CartService;
use App\Interfaces\OrderService;
use App\Interfaces\CouponService;

use App\Models\Goods;
use App\Models\Order;
use App\Models\User;
use App\Models\Address;

use Auth,Log;



class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(AddressService $addressService,OrderService $orderService, CouponService $couponService, Request $request)
    {

        //1 获取选中的商品

        $from = $request->get('from');

        if ($from=='detail'){
            $cartGoods = Session::get('fastPay');

        }else{
            $cartGoods = Session::get('cartGoods');
        }

        //获取收货地址

        $defaultAddress = $addressService->getUserDefaultAddress(Auth::user()->id);
        if($defaultAddress){
            $info = $orderService->getBuyGoodsInfo($cartGoods,$defaultAddress->address_id,[],Auth::user()->id);

            $default_address_id = $defaultAddress->address_id;

        }else{
            $info = $orderService->getBuyGoodsInfo($cartGoods,0,[],Auth::user()->id);

            $default_address_id=null;
        }
// dd($info['orders']['eshopGoods']['discounts']->toArray());
        $address = $addressService->getUserAddress(Auth::user()->id);

        //直辖市
        // $cities = ['北京市', '天津市', '上海市', '重庆市'];

        $cities = config('address.cities');


        //优惠券
        $user_id = Auth::user()->id;
        $goods_id_arr = [];
        foreach ($cartGoods as $key => $value) {
            $goods_id = $value['goods_id'];
            $goods_id_arr[$key] = $value['goods_id'];
        }//end foreach()

// dd($info);
        return view('mobile.order.index',compact(['info','address','from','default_address_id', 'user_coupons', 'cities']))
            ->withHeader('Cache-control', 'no-cache,no-store,must-revalidate')
            ->withHeader('Pragma','no-cache')
            ->withHeader('Expires','0');

    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, OrderService $orderService,CartService $cartService)
    {

        $return = ['status'=>1, 'message'=>'', 'data'=>''];

        $user_id = Auth::user()->id;

        $goods = $request->goods;

        $address_id = $request->address_id;

        $pay_type = $request->payType;

        $is_all_goods_exception = isset($request->is_all_goods_exception) ? $request->is_all_goods_exception : 0 ;

        if (empty($user_id)) {

            $return['status'] = 0;

            $return['message'] = '用户id不能为空';

            return $return;
        }//end if()

        if (empty($goods)) {

            $return['status'] = 0;

            $return['message'] = '购买商品不能为空';

            return $return;
        }//end if()

        if (empty($address_id)) {

            $return['status'] = 0;

            $return['message'] = '收货地址不能为空';

            return $return;
        }else{

           $address = Address::find($address_id);

           if (empty($address)) {

                $return['status'] = 0;

                $return['message'] = '收货地址不存在';

                return $return;
           }
        }//end if()

        //优惠券
        $coupons = [
            'e_coupon_id'=>$request->get('e_coupon_id'),
            'c_coupon_id'=>$request->get('c_coupon_id')
        ];

        if (empty($pay_type)) {

            $return['status'] = 0;

            $return['message'] = '支付类型不能为空';

            return $return;
        }//end if()

        try {

            $order_ids = $orderService->createOrder($user_id, $goods, $address_id, $coupons, 1, $is_all_goods_exception, $pay_type);

            if (!empty($order_ids)) {

                 $return = $order_ids;
            }//end if()

            //清空购物车所选商品
            if (empty($request->from)){

                $cartService->init('USER', $user_id);

                $cartService->removeCartGoods($goods);
            }

            return response()->json($return);

        } catch (OrderServiceException $e) {

            $return['status'] = 0;

            $return['message'] = $e->getMessage();

            return $return;

        } catch (OrderGoodsServiceException $e){

            $return['status'] = 0;

            $return['message'] = $e->getMessage();

            $return['data'] = $e->getData();

            return $return;
        }

    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($address_id,AddressService $addressService,OrderService $orderService,Request $request)
    {

        //1 获取选中的商品

        $from = $request->get('from');

        if ($from=='detail'){

            $cartGoods = Session::get('fastPay');
        }else{
            $cartGoods = Session::get('cartGoods');
        }

        //优惠券
        $coupons = [
            'e_coupon_id'=>$request->get('e_coupon_id'),
            'c_coupon_id'=>$request->get('c_coupon_id')
        ];

        //限时折扣
        /*$discounts = [
            'e_discount_id'=>$request->get('e_discount_id'),
            'c_discount_id'=>$request->get('c_discount_id')
        ];


        $current_discount_arr=[];

        if (@$request->get('discount_type')) {

            $current_discount_arr['discount_type'] = $request->get('discount_type');

            $current_discount_arr['goods_number'] = $request->get('goods_number');

            switch ($request->get('discount_type')) {

                case 'e':
                    $current_discount_arr['discount_id'] = $request->get('e_discount_id');
                    break;

                case 'c':
                    $current_discount_arr['discount_id'] = $request->get('c_discount_id');
                    break;
            }
        }*/

        //获取收货地址
        // $address = $addressService->getUserAddress(Auth::user()->id);

        $info = $orderService->getBuyGoodsInfo($cartGoods,$address_id,$coupons,Auth::user()->id);
        // $info = $orderService->getBuyGoodsInfo($cartGoods,$address_id,$coupons,$discounts,$current_discount_arr,Auth::user()->id);

        return response()->json(['info'=>$info]);
    }

}
